var forbidword=["http","https","www","com","cn","net","@","qq","wx","微信","联系","操","你妈","兼职","电话","://"];
var noteapi = {
  submitNoteApi: function (that, uinfo) {
    that.showToast('提交中', 'loading');
    try {
      if (that.data.textarealength < 5) {
        that.showToast('留言至少5个字', 'none');
        return;
      }
      for(var i=0;i<forbidword.length;i++){
       
        if(that.data.textdata.includes(forbidword[i])){
          that.showToast('疑似有非法内容请编辑后再发布', 'none');
          return;
        }
      }
      if(/^[0-9]+$/.test(that.data.textdata)){
        that.showToast('疑似有非法内容请编辑后再发布', 'none');
        return;
      }
      if (uinfo.detail.userInfo) {
       
        wx.cloud.callFunction({
          name: 'submitnote',
          data: {
            target: that.data.id,
            type: that.data.type,
            msgid: (Math.random() * 10).toString(36).substr(6),
            score: that.data.score,
            text: that.data.textdata,
            time: "",
            nickName: uinfo.detail.userInfo.nickName,
            avatarUrl: uinfo.detail.userInfo.avatarUrl
          },
          success: res => {
            if (!res.result){
              that.showToast("发布成功", 'none');
            }else{
              that.showToast(res.result, 'none');
            }
            that.setData({ textdata: "" });
            that.setData({ textarealength: 0 });
            noteapi.getnote(that);
          },
          fail: err => {
            that.showToast('发布失败', 'none');
          }
        })
      } else {
        that.showToast('获取用户名失败', 'none');
      }
    } catch (e) {
     
      that.showToast('提交失败', 'none');
    }
  },
  getnote: function (that) {
    wx.cloud.callFunction({
      name: 'getnote',
      data: {
        target: that.data.id
      },
      success: res => {
        if (res.result.data) {
          if (res.result.data.length==0) {
            //that.showToast('还没有人留言，当第一个留言的人吧！', 'none');
          }
          for (var i in res.result.data) {
            res.result.data[i].time = formatUTC(res.result.data[i].time)
          }
          that.setData({ notedata: res.result })
        } else {
          //that.showToast('获取评论失败', 'none');
        }
      },
      fail: err => {
        //that.showToast('获取评论失败', 'none');
      }
    })
  },
  getallnote: function (that) {
    wx.cloud.callFunction({
      name: 'getallnote',
      data: {
        target: that.data.id
      },
      success: res => {
        if (res.result.data) {
          if (res.result.data.length == 0) {
            that.showToast('还没有人留言，当第一个留言的人吧！', 'none');
          }
          for (var i in res.result.data) {
            res.result.data[i].time = formatUTC(res.result.data[i].time)
          }
          that.setData({ notedata: res.result })
        } else {
          //that.showToast('获取评论失败', 'none');
        }
      },
      fail: err => {
        //that.showToast('获取评论失败', 'none');
      }
    })
  },
  getunitnote: function (that) {
    wx.cloud.callFunction({
      name: 'getunitnote',
      data: {},
      success: res => {
        
        if (res.result.data) {
          if (res.result.data.length == 0) {
            that.showToast('还没有人留言，当第一个留言的人吧！', 'none');
          }
          for (var i in res.result.data) {
            res.result.data[i].time = formatUTC(res.result.data[i].time)
          }
          that.setData({ notedata: res.result })
        } else {
          //that.showToast('获取评论失败', 'none');
        }
      },
      fail: err => {
        //that.showToast('获取评论失败', 'none');
      }
    })
  },
  getpilotskill: function (that) {
    wx.cloud.callFunction({
      name: 'getpilotskill',
      success: res => {
        if (res.result.data) {
          return res.result.data
        }else{
          that.showToast('获取数据失败', 'none');
        }
      },
      fail: err => {
        //that.showToast('获取评论失败', 'none');
      }
    })
  }
}

Date.prototype.Format = function (formatStr) {
  var str = formatStr;
  var Week = ['日', '一', '二', '三', '四', '五', '六'];
  var month = this.getMonth() + 1;
  str = str.replace(/yyyy|YYYY/, this.getFullYear());
  str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
  str = str.replace(/MM/, month > 9 ? month.toString() : '0' + month);
  str = str.replace(/M/g, this.getMonth());
  str = str.replace(/w|W/g, Week[this.getDay()]);
  str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
  str = str.replace(/d|D/g, this.getDate());
  str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
  str = str.replace(/h|H/g, this.getHours());
  str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
  str = str.replace(/m/g, this.getMinutes());
  str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
  str = str.replace(/s|S/g, this.getSeconds());
  return str;
}

function formatUTC(utc_datetime) {
  // 转为正常的时间格式 年-月-日 时:分:秒
  var T_pos = utc_datetime.indexOf('T');
  var Z_pos = utc_datetime.indexOf('Z');
  var year_month_day = utc_datetime.substr(0, T_pos);
  var hour_minute_second = utc_datetime.substr(T_pos + 1, Z_pos - T_pos - 5);
  var new_datetime = year_month_day + " " + hour_minute_second;
  return new_datetime;

  // // 处理成为时间戳
  // timestamp = new Date(Date.parse(new_datetime));
  // timestamp = timestamp.getTime();
  // timestamp = timestamp / 1000;

  // // 增加8个小时，北京时间比utc时间多八个时区
  // var timestamp = timestamp + 8 * 60 * 60;

  // // 时间戳转为时间
  // var beijing_datetime = new Date(parseInt(timestamp) * 1000).Format("YYYY/MM/DD hh:mm:ss")
  
}



module.exports = noteapi;